package com.think.oauth.user.service;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.think.common.utils.RandomUtils;
import com.think.oauth.user.constant.EmailType;
import com.think.oauth.user.model.User;

import lombok.val;

@Service
public class SendMailService {
	 //获取redis模板类
    @Autowired
    RedisTemplate redisTemplate;
      //获取邮件发送类
    @Autowired
    JavaMailSender javaMailSender;
    
    @Value("${spring.mail.username}")
    private String email;
    
    private String registerTemplate = "感谢您注册加速想象力虚拟仿真平台账号，这个是您的验证码:$code,有效期为5分钟"; 
    
    private String retriveTemplate = "您正在使用邮箱找回密码，这个是您的验证码:$code,有效期为5分钟";

     //异步请求
    @Async
    public void sendMail(String emailAddress,EmailType type){
        //初始化邮件信息类
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
       
        simpleMailMessage.setSubject("虚拟仿真教学平台验证码");
        simpleMailMessage.setFrom(email);//输入你的QQ邮箱
        simpleMailMessage.setTo(emailAddress);
        String content = "";
        String verification = RandomUtils.getRandomString("ABCDEFGHJKLMNPQRSTUVWXYZ01235467899", 6);

		content = registerTemplate.replace("$code", verification);

        //获取验证码
        //将验证码存放进邮箱
        simpleMailMessage.setText(content);
        //获取redis操作类
        ValueOperations valueOperations = redisTemplate.opsForValue();
        /*设置缓存*/
        valueOperations.set(emailAddress,verification);
        redisTemplate.expire(emailAddress,60*5, TimeUnit.SECONDS);
        javaMailSender.send(simpleMailMessage);
    }
    
    @Async
    public void sendRetriveMail(String emailAddress){
        //初始化邮件信息类
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
       
        simpleMailMessage.setSubject("虚拟仿真教学平台验证码");
        simpleMailMessage.setFrom(email);//输入你的QQ邮箱
        simpleMailMessage.setTo(emailAddress);
        
        //获取验证码
        String verification = RandomUtils.getRandomString("ABCDEFGHJKLMNPQRSTUVWXYZ01235467899", 6);
		String content = retriveTemplate.replace("$code", verification);

        //将验证码存放进邮箱
        simpleMailMessage.setText(content);
        //获取redis操作类
        ValueOperations valueOperations = redisTemplate.opsForValue();
        System.out.println(email + "-" + verification);
        /*设置缓存*/
        valueOperations.set(emailAddress+"_retrive",verification);
        redisTemplate.expire(emailAddress,60*5, TimeUnit.SECONDS);
        javaMailSender.send(simpleMailMessage);

    }
    //验证用户信息
    public Boolean verification(String email,String code){
        ValueOperations valueOperations = redisTemplate.opsForValue();
        /*从redis中获取验证码*/
        String verificationCode = (String) valueOperations.get(email);
        System.out.println(verificationCode);
        /*判断提交的信息是否正确*/
        if(verificationCode!=null&&verificationCode.equals(code)){
            /*删除缓存中的数据*/
            redisTemplate.delete(email);
            return true;
        }
        return false;
    }
    
  //验证用户信息
    public Boolean verificationRetrive(String email,String code){
        ValueOperations valueOperations = redisTemplate.opsForValue();
        /*从redis中获取验证码*/
        String verificationCode = (String) valueOperations.get(email+"_retrive");
        System.out.println(verificationCode);
        /*判断提交的信息是否正确*/
        if(verificationCode!=null&&verificationCode.equals(code)){
            /*删除缓存中的数据*/
            redisTemplate.delete(email);
            return true;
        }
        return false;
    }
}
